﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;

using System.Runtime.InteropServices;
using Lamex.Library.CommonClass;
using Lamex.Interface;

using Lamex.mCapacity.UI;
using Lamex.mSchedule.UI;
using Lamex.mEfficiency.UI;
using Lamex.mMaterial.UI;
using Lamex.mMachine.UI;
using Lamex.mKanban.UI;
using DevExpress.XtraBars.Alerter;

namespace Lamex.mMES.UI
{
    public partial class frmMES_Operation_Function_Demo : frmBaseChild//Form//
    {
        public static Form fm = null;
        public static string strPic;
        public static int X,Y;

        string strRGB;

        Lamex.mMES.ucControls.ucShowPic ucShowPicList = null;
        //如何利用控件完成Winforms图形化界面设计
        //https://www.devexpresscn.com/post/641.html

        //素材文件路径：\\Tx-fs10\tx-fs1\LAMEX\MES\Resources，工具：MindMaster

        [DllImport("gdi32.dll")]
        static public extern uint GetPixel(IntPtr hDC, int XPos, int YPos);
        [DllImport("gdi32.dll")]
        static public extern IntPtr CreateDC(string driverName, string deviceName, string output, IntPtr lpinitData);
        [DllImport("gdi32.dll")]
        static public extern bool DeleteDC(IntPtr DC);

        static public byte GetRValue(uint color)
        {
            return (byte)color;
        }
        static public byte GetGValue(uint color)
        {
            return ((byte)(((short)(color)) >> 8));
        }
        static public byte GetBValue(uint color)
        {
            return ((byte)((color) >> 16));
        }
        static public byte GetAValue(uint color)
        {
            return ((byte)((color) >> 24));
        }

        public Color GetColor(Point screenPoint)
        {
            IntPtr displayDC = CreateDC("DISPLAY", null, null, IntPtr.Zero);
            uint colorref = GetPixel(displayDC, screenPoint.X, screenPoint.Y);
            DeleteDC(displayDC);
            byte Red = GetRValue(colorref);
            byte Green = GetGValue(colorref);
            byte Blue = GetBValue(colorref);
            return Color.FromArgb(Red, Green, Blue);
        }   

        public frmMES_Operation_Function_Demo()
        {
            InitializeComponent();

            this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
            this.pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
            this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

            //List<string> listPic = new List<string>();
            //listPic.Add(@"D:\LamexMES\Lamex.mMES\Resources\Layout_Laminate_Down.png");
            //string list = @"\\Tx-fs10\tx-fs1\LAMEX\MES\Resources\MES_OPRATION.jpg";
            //Bitmap myBmp = new Bitmap(list);//图片定义
            //ucShowPicList = new ucControls.ucShowPic();
            //ucShowPicList.LoadImageList(myBmp);
            //ucShowPicList.Dock = DockStyle.Fill;
            //panelControl1.Controls.Add(ucShowPicList);
        }

        private void frmMES_Operation_Function_Demo_Resize(object sender, EventArgs e)
        {
            pictureBox1.Width = panel1.Width;
            pictureBox1.Height = panel1.Height;
            pictureBox1.Load(@"\\Tx-fs10\tx-fs1\LAMEX\MES\Resources\MES_Operation.jpg");//白底
            //pictureBox1.Load(@"\\Tx-fs10\tx-fs1\LAMEX\MES\Resources\MES_Operation.png");//皮肤底
        }

        #region 图片放大缩小平移

        private bool isMouseDown = false;
        private Point mouseOffset;

        private void panel1_MouseWheel(object sender, MouseEventArgs e)
        {
            double step = 1.2;
            if (e.Delta > 0)
            {
                if (pictureBox1.Height >= Screen.PrimaryScreen.Bounds.Height * 10)
                    return;
                pictureBox1.Height = (int)(pictureBox1.Height * step);
                pictureBox1.Width = (int)(pictureBox1.Width * step);

                int px = Cursor.Position.X - pictureBox1.Location.X;
                int py = Cursor.Position.Y - pictureBox1.Location.Y;
                int px_add = (int)(px * (step - 1.0));
                int py_add = (int)(py * (step - 1.0));
                pictureBox1.Location = new Point(pictureBox1.Location.X - px_add, pictureBox1.Location.Y - py_add);
                Application.DoEvents();
            }
            else
            {
                pictureBox1.Height = (int)(pictureBox1.Height / step);
                pictureBox1.Width = (int)(pictureBox1.Width / step);

                int px = Cursor.Position.X - pictureBox1.Location.X;
                int py = Cursor.Position.Y - pictureBox1.Location.Y;
                int px_add = (int)(px * (1.0 - 1.0 / step));
                int py_add = (int)(py * (1.0 - 1.0 / step));
                pictureBox1.Location = new Point(pictureBox1.Location.X + px_add, pictureBox1.Location.Y + py_add);
                Application.DoEvents();
            }
        }

        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            double step = 1.2;
            if (e.Delta > 0)
            {
                if (pictureBox1.Height >= Screen.PrimaryScreen.Bounds.Height * 10)
                    return;
                pictureBox1.Height = (int)(pictureBox1.Height * step);
                pictureBox1.Width = (int)(pictureBox1.Width * step);

                int px = Cursor.Position.X - pictureBox1.Location.X;
                int py = Cursor.Position.Y - pictureBox1.Location.Y;
                int px_add = (int)(px * (step - 1.0));
                int py_add = (int)(py * (step - 1.0));
                pictureBox1.Location = new Point(pictureBox1.Location.X - px_add, pictureBox1.Location.Y - py_add);
                Application.DoEvents();
            }
            else
            {
                pictureBox1.Height = (int)(pictureBox1.Height / step);
                pictureBox1.Width = (int)(pictureBox1.Width / step);

                int px = Cursor.Position.X - pictureBox1.Location.X;
                int py = Cursor.Position.Y - pictureBox1.Location.Y;
                int px_add = (int)(px * (1.0 - 1.0 / step));
                int py_add = (int)(py * (1.0 - 1.0 / step));
                pictureBox1.Location = new Point(pictureBox1.Location.X + px_add, pictureBox1.Location.Y + py_add);
                Application.DoEvents();
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                int left = pictureBox1.Left + e.X - mouseOffset.X;
                int top = pictureBox1.Top + e.Y - mouseOffset.Y;
                pictureBox1.Location = new Point(left, top);
            }
            //不能在MouseMove事件中关闭，因为打开时间
            if (fm != null)
            {
                //没调好不起作用
                timer1.Start();
                timer1.Stop();
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = false;
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOffset.X = e.X;
                mouseOffset.Y = e.Y;
                isMouseDown = true;

                if (e.Clicks == 1)
                {
                    //直接处理单击用户操作，
                    //通过e.Clicks方式区分单双击也不彻底，双击速度不够快时，还是会触发单击事件，暂时这样
                    OpenOperationAnimateWindow();
                }
                //if (e.Clicks == 2)
                //{
                //    //双击事件
                //}
            }
            else if (e.Button == MouseButtons.Right)//右键（右击）
            {
                pictureBox1.Width=panel1.Width;
                pictureBox1.Height = panel1.Height;
                pictureBox1.Location = new Point(0, 0);
            }
        }

        #endregion

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            getPointRgbVlue();

            //MessageBox.Show(strRGB);
            //return;

            #region 跳转相应业务模块

            switch (strRGB)
            {
                case "254,2,1"://流程
                    MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMES_Operation_Management), sender as ToolStripMenuItem);
                    break;
                case "124,0,124"://项目
                    MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMES_Project_Schedule), sender as ToolStripMenuItem);
                    break;
                case "2,0,135"://KPI
                    MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMES_Operation_KPI), sender as ToolStripMenuItem);
                    break;
                case "199,100,45"://日历
                    MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMES_Operation_Digital_Calendar), sender as ToolStripMenuItem);
                    break;
                case "4,128,4"://现场
                    MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMES_Operation_Workshop_Overview), sender as ToolStripMenuItem);
                    break;
                case "12,143,138"://产能
                    MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmCAP_Owed_Analysis_Summarize), sender as ToolStripMenuItem);
                    break;
                case "254,109,2"://进度
                    MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmSCH_Position_Search), sender as ToolStripMenuItem);
                    break;
                case "221,89,136"://效率
                    MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmEFF_Efficiency_Analyse), sender as ToolStripMenuItem);
                    break;
                case "119,153,43"://物料
                    MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMC_MO_BOM), sender as ToolStripMenuItem);
                    break;
                case "38,38,170"://看板
                    MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmKan_Accessory), sender as ToolStripMenuItem);
                    break;
                case "226,80,31"://出货
                    MessageBox.Show("待开发！");
                    break;
                case "121,51,114"://消耗品
                    MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmFAMA_WarPow_Data_Analysis), sender as ToolStripMenuItem);
                    break;
                default:
                    MessageBox.Show("请双击圆形标记中有颜色区域！" + "\r\n" + "开始、结束、其他不适用！");
                    break;
            }

            #endregion
        }

        private string getPointRgbVlue()
        {
            //txtPoint.Text = Control.MousePosition.X.ToString() + "," + Control.MousePosition.Y.ToString();
            Point pt = new Point(Control.MousePosition.X, Control.MousePosition.Y);

            //Point p = Control.MousePosition;
            X = pt.X + 10;
            Y = pt.Y + 10;
                        
            Color cl = GetColor(pt);
            //panel1.BackColor = cl;
            //txtRGB.Text = cl.R + "," + cl.G + "," + cl.B;
            //txtColor.Text = ColorTranslator.ToHtml(cl).ToString();
            //RegisterHotKey(Handle, 81, KeyModifiers.Ctrl, Keys.F);

            strRGB = cl.R.ToString() + "," + cl.G.ToString() + "," + cl.B.ToString();

            return strRGB;
        }

        //private void pictureBox1_Click(object sender, EventArgs e)
        //{
        //    //因Mouse_Hover事件不稳定，所以同时提供单击操作
        //    //因鼠标双击事件会触发单击事件，已改到_MouseDown事件中处理
        //    //OpenOperationAnimateWindow();
        //}

        //禁用MouseHover事件，因事件触发过于频繁，干扰用户操作
        //改为由单击操作触发
        //private void pictureBox1_MouseHover(object sender, EventArgs e)
        //{
        //    OpenOperationAnimateWindow();
        //}

        private void OpenOperationAnimateWindow()
        {
    
            #region 获取提示图片名称

            getPointRgbVlue();
            strPic = "";

            switch (strRGB)
            {
                case "254,2,1"://流程
                    strPic = "流程";
                    break;
                case "124,0,124"://项目
                    strPic = "项目";
                    break;
                case "2,0,135"://KPI
                    strPic = "KPI";
                    break;
                case "199,100,45"://日历
                    strPic = "日历";
                    break;
                case "4,128,4"://现场
                    strPic = "现场";
                    break;
                case "12,143,138"://产能
                    strPic = "产能";
                    break;
                case "254,109,2"://进度
                    strPic = "进度";
                    break;
                case "221,89,136"://效率
                    strPic = "效率";
                    break;
                case "119,153,43"://物料
                    strPic = "物料";
                    break;
                case "38,38,170"://看板
                    strPic = "看板";
                    break;
                case "226,80,31"://出货
                    strPic = "出货";
                    break;
                case "121,51,114"://消耗品
                    strPic = "消耗品";
                    break;
                default:
                    break;
            }

            #endregion

            if (strPic != "")
            {
                this.Cursor = System.Windows.Forms.Cursors.Help;
                if (fm == null)
                {
                    strPic = @"\\Tx-fs10\tx-fs1\LAMEX\MES\Resources\" + strPic;
                    frmMES_Operation_AnimateWindow fm2 = new frmMES_Operation_AnimateWindow();
                    fm2.Show();
                }
                else
                {
                    fm.Close();
                    fm = null;
                    //没调好不起作用
                    //timer1.Start();
                    //timer1.Stop();
                }
            }
            else
            {
                this.Cursor = System.Windows.Forms.Cursors.Arrow;
            }
        }

        //没调好不起作用
        private void timer1_Tick(object sender, EventArgs e)
        {
            fm.Close();
            fm = null;
        }

        #region 鼠标滑过说明标签是显示相关信息或操作

        //AlertControl警示控件——如QQ右下角弹出消息提示，过一段时间消失
        //https://blog.csdn.net/archielau/article/details/37578327?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

        private void labelControl1_MouseHover(object sender, EventArgs e)
        {
            alertControl1.FormLocation = DevExpress.XtraBars.Alerter.AlertFormLocation.BottomRight;//提出窗口的显示的位置
            alertControl1.AutoFormDelay = 5000;//设置显示多少时间窗口会自动关闭
            //出现的效果方式
            alertControl1.FormShowingEffect = DevExpress.XtraBars.Alerter.AlertFormShowingEffect.FadeIn;
            //弹出的速度
            alertControl1.FormDisplaySpeed = DevExpress.XtraBars.Alerter.AlertFormDisplaySpeed.Slow;
            alertControl1.Show(this, "提示!", "*双击圆形颜色区，跳转到相应操作模块!" + "\r\n" + 
                "滚动鼠标，图片放大缩小；" + "\r\n" +
                "右击鼠标，图片复位；" + "\r\n" +
                "左击鼠标，模块预览；" + "\r\n" +
                "单击提示窗体，关闭提示窗体。");

            //双击圆形颜色区，跳转到相应操作模块
        }

        private void alertControl1_FormLoad(object sender, DevExpress.XtraBars.Alerter.AlertFormLoadEventArgs e)
        {
            //string strPath = System.Windows.Forms.Application.StartupPath;

            e.AlertForm.Size = new System.Drawing.Size(300, 150);

 
			// Show a sample alert window.
            //AlertInfo info = new AlertInfo("New Window", "Text");
            //alertControl1.Show(this, info);	
        }

        private void alertControl1_BeforeFormShow(object sender, DevExpress.XtraBars.Alerter.AlertFormEventArgs e)
        {
            //同FormLoad事件中大小的参数值一定要相同
            e.AlertForm.Size = new System.Drawing.Size(300, 150);
        }

		void alertControl1_ButtonClick(object sender, AlertButtonClickEventArgs e)
		{
            if (e.ButtonName == "buttonOpen")
            {
                MessageBox.Show("buttonOpen");
            }
        }

        void alertControl1_ButtonDownChanged(object sender, AlertButtonDownChangedEventArgs e)
        {
            if (e.ButtonName == "buttonAlert")
            {
                MessageBox.Show("buttonAlert");
            }
        }
 
        #endregion

        private void labelControl2_Click(object sender, EventArgs e)
        {
            MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMES_Operation_PPT), sender as ToolStripMenuItem);
        }

    }
}